package com.example.task1.ui.AllService.subway;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.SubwayMyCartListBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class SubwayCartListAdapter extends BaseAdapter<SubwayMyCartListBean.DataDTO> {

    private Context context;
    private List<SubwayMyCartListBean.DataDTO> mData = new ArrayList<>();
    private TextView tvName;
    private TextView tvIdCard;
    private TextView tvPhonenumber;
    private TextView tvCardNum;
    private TextView tvTime;
    private Button btnRemove;

    public SubwayCartListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<SubwayMyCartListBean.DataDTO> rows) {
        mData.clear();
        mData = rows;
        notifyDataSetChanged();
    }

    @Override
    protected int getLayout() {
        return R.layout.subwaycartlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<SubwayMyCartListBean.DataDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, SubwayMyCartListBean.DataDTO date) {

    }

    @Override
    protected void onBind(myHolder holder, SubwayMyCartListBean.DataDTO date, int position) {
        tvCardNum.setText(date.getCardNum());
        tvIdCard.setText(date.getIdCard());
        tvName.setText(date.getUserName());
        tvPhonenumber.setText(date.getPhonenumber());
        tvTime.setText(date.getCreateTime());
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ApiResp> del = Api.api().del(MyApplication.TOKEN, "/prod-api/api/metro/card/" + date.getId());
                Api.send(del, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        mData.remove(position);
                        notifyItemRemoved(position);
                        MyApplication.show("解绑成功");
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View view) {
        initView(view);
    }

    private void initView(View view) {
        tvName = view.findViewById(R.id.tv_name);
        tvIdCard = view.findViewById(R.id.tv_idCard);
        tvPhonenumber = view.findViewById(R.id.tv_phonenumber);
        tvCardNum = view.findViewById(R.id.tv_cardNum);
        tvTime = view.findViewById(R.id.tv_time);
        btnRemove = view.findViewById(R.id.btn_remove);
    }
}
